package com.cardbaobao.cardbabyclient.ui.activity;

import android.content.Intent;
import android.os.Bundle;
import android.text.Editable;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.cardbaobao.cardbabyclient.R;
import com.cardbaobao.cardbabyclient.api.Api;
import com.cardbaobao.cardbabyclient.base.Constant;
import com.cardbaobao.cardbabyclient.buried.Self;
import com.cardbaobao.cardbabyclient.model.CardBin;
import com.cardbaobao.cardbabyclient.model.result.CardBinResult;
import com.cardbaobao.cardbabyclient.ui.activity.base.BaseActivity;
import com.cardbaobao.cardbabyclient.utils.ApplicationHelper;
import com.cardbaobao.cardbabyclient.buried.GoogleAnalyticsUtil;
import com.cardbaobao.cardbabyclient.utils.JsonUtil;
import com.cardbaobao.cardbabyclient.utils.RegularUtil;
import com.cardbaobao.cardbabyclient.utils.ToastUtil;
import com.cardbaobao.cardbabyclient.widget.TitleBar;

import butterknife.BindView;
import butterknife.OnClick;
import butterknife.OnTextChanged;
import rx.Subscriber;

/**
 * Created by Jiangzx on 2017/9/13 19:25.
 * 更换银行卡
 */

public class ReplaceCardActivity extends BaseActivity {
    public static final String NAME_S ="name";//姓名
    public static final String ID_CARD_NO_S ="idCardNo";//身份证号码
    public static final String CARD_NUMBER_S="cardNumber";//银行卡号码
    @BindView(R.id.title_bar)
    TitleBar titleBar;
    @BindView(R.id.tv_card_show)
    TextView tvCardShow;
    @BindView(R.id.tv_real_name)
    TextView tvRealName;
    @BindView(R.id.et_card_number)
    EditText etCardNumber;

    @BindView(R.id.bt_next_step)
    Button btNextStop;

    private String name;
    private String idCardNo;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_replace_card;
    }

    @Override
    protected void initView(Bundle savedInstanceState) {
        super.initView(savedInstanceState);
        titleBar.setLeftViewOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });

        mCurrentPageId= Self.Page.app_replace_debit_card_page;
    }

    @Override
    protected void initData(Bundle savedInstanceState) {
        super.initData(savedInstanceState);
        Intent intent=getIntent();
        name=intent.getStringExtra(NAME_S);
        idCardNo=intent.getStringExtra(ID_CARD_NO_S);
        tvRealName.setText(name);
    }

    @Override
    protected void onResume() {
        super.onResume();
        GoogleAnalyticsUtil.setScreenName("更换银行卡");
    }

    @OnTextChanged({R.id.et_card_number})
    public void afterTextChange(Editable s){
        if(s.length()>0){
            StringBuilder sb=new StringBuilder();
            for(int i=0,length=s.length();i<length;i++){
                if(i>0&&i%4==0){
                    sb.append(" ");
                    sb.append(s.charAt(i));
                }else{
                    sb.append(s.charAt(i));
                }
            }
            tvCardShow.setText(sb.toString());

            if(RegularUtil.checkBankCard(s.toString())){
                btNextStop.setEnabled(true);
            }else{
                btNextStop.setEnabled(false);
            }
        }else{
            tvCardShow.setText("");
            btNextStop.setEnabled(false);
        }
    }

    private void queryCardBin(final String cardNumber){
        showProgressDialog(false);
        Api.queryCardBin(cardNumber).subscribe(new Subscriber<String>() {
            @Override
            public void onCompleted() {

            }

            @Override
            public void onError(Throwable throwable) {
                dismissProgressDialog();
                ToastUtil.showToast(ApplicationHelper.getApplicationContext().
                        getString(R.string.string_request_server_fail));
            }

            @Override
            public void onNext(String s) {
                dismissProgressDialog();
                CardBinResult result= JsonUtil.json2Object(s,CardBinResult.class);
                if(result.getCode()== Constant.SUC_CODE){
                    //成功
                    CardBin bin=result.getData();
                    if("1".equals(bin.getBank_type())){
                        //储蓄卡
                        initIntent();
                        mIntent.setClass(mContext,BankCardVerifyActivity.class);
                        mIntent.putExtra(NAME_S,name);
                        mIntent.putExtra(ID_CARD_NO_S,idCardNo);
                        mIntent.putExtra(CARD_NUMBER_S,cardNumber);
                        mIntent.putExtra(BankCardVerifyActivity.CARD_NAME_S,
                                bin.getBank_name());
                        mIntent.putExtra(BankCardVerifyActivity.CARD_ID_S,
                                bin.getBank_id());
                        mIntent.putExtra(BankCardVerifyActivity.PAGE_FROM_I,
                                BankCardVerifyActivity.ACTIVITY_CARD_MANAGE_REPLACE);
                        startActivity(mIntent);
                        finish();
                    }
                }else{
                    ToastUtil.showToast(result.getMsg());
                }
            }
        });
    }

    @OnClick({R.id.bt_next_step})
    public void onClick(View view){
        switch (view.getId()){
            case R.id.bt_next_step:
                queryCardBin(etCardNumber.getText().toString().trim());
                break;
        }
    }
}
